home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / idparam.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.3 KB  |  399 lines

  1. /* Copyright (C) 1992, 1995, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: idparam.c,v 1.3 2000/09/19 19:00:43 lpd Exp $ */
  20. /* Utilities for getting parameters out of dictionaries. */
  21. #include "memory_.h"
  22. #include "string_.h"        /* for strlen */
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "gsmatrix.h"        /* for dict_matrix_param */
  26. #include "gsuid.h"
  27. #include "idict.h"
  28. #include "idparam.h"        /* interface definition */
  29. #include "ilevel.h"
  30. #include "imemory.h"        /* for iutil.h */
  31. #include "iname.h"
  32. #include "iutil.h"
  33. #include "oper.h"        /* for check_proc */
  34. #include "store.h"        /* for making empty proc */
  35.  
  36. /* Get a Boolean parameter from a dictionary. */
  37. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  38. int
  39. dict_bool_param(const ref * pdict, const char *kstr,
  40.         bool defaultval, bool * pvalue)
  41. {
  42.     ref *pdval;
  43.  
  44.     if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  45.     *pvalue = defaultval;
  46.     return 1;
  47.     }
  48.     if (!r_has_type(pdval, t_boolean))
  49.     return_error(e_typecheck);
  50.     *pvalue = pdval->value.boolval;
  51.     return 0;
  52. }
  53.  
  54. /* Get an integer or null parameter from a dictionary. */
  55. /* Return 0 if found, 1 if defaulted, <0 if invalid. */
  56. /* If the parameter is null, return 2 without setting *pvalue. */
  57. /* Note that the default value may be out of range, in which case */
  58. /* a missing value will return e_rangecheck rather than 1. */
  59. int
  60. dict_int_null_param(const ref * pdict, const char *kstr, int minval,
  61.             int maxval, int defaultval, int *pvalue)
  62. {
  63.     ref *pdval;
  64.     int code;
  65.     long ival;
  66.  
  67.     if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  68.     ival = defaultval;
  69.     code = 1;
  70.     } else {
  71.     switch (r_type(pdval)) {
  72.         case t_integer:
  73.         ival = pdval->value.intval;
  74.         break;
  75.         case t_real:
  76.         /* Allow an integral real, because Fontographer */
  77.         /* (which violates the Adobe specs in other ways */
  78.         /* as well) sometimes generates output that */
  79.         /* needs this. */
  80.         if (pdval->value.realval < minval || pdval->value.realval > maxval)
  81.             return_error(e_rangecheck);
  82.         ival = (long)pdval->value.realval;
  83.         if (ival != pdval->value.realval)
  84.             return_error(e_rangecheck);
  85.         break;
  86.         case t_null:
  87.         return 2;
  88.         default:
  89.         return_error(e_typecheck);
  90.     }
  91.     code = 0;
  92.     }
  93.     if (ival < minval || ival > maxval)
  94.     return_error(e_rangecheck);
  95.     *pvalue = (int)ival;
  96.     return code;
  97. }
  98. /* Get an integer parameter from a dictionary. */
  99. /* Return like dict_int_null_param, but return e_typecheck for null. */
  100. int
  101. dict_int_param(const ref * pdict, const char *kstr, int minval, int maxval,
  102.            int defaultval, int *pvalue)
  103. {
  104.     int code = dict_int_null_param(pdict, kstr, minval, maxval,
  105.                    defaultval, pvalue);
  106.  
  107.     return (code == 2 ? gs_note_error(e_typecheck) : code);
  108. }
  109.  
  110. /* Get an unsigned integer parameter from a dictionary. */
  111. /* Return 0 if found, 1 if defaulted, <0 if invalid. */
  112. /* Note that the default value may be out of range, in which case */
  113. /* a missing value will return e_rangecheck rather than 1. */
  114. int
  115. dict_uint_param(const ref * pdict, const char *kstr,
  116.         uint minval, uint maxval, uint defaultval, uint * pvalue)
  117. {
  118.     ref *pdval;
  119.     int code;
  120.     uint ival;
  121.  
  122.     if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  123.     ival = defaultval;
  124.     code = 1;
  125.     } else {
  126.     check_type_only(*pdval, t_integer);
  127.     if (pdval->value.intval != (uint) pdval->value.intval)
  128.         return_error(e_rangecheck);
  129.     ival = (uint) pdval->value.intval;
  130.     code = 0;
  131.     }
  132.     if (ival < minval || ival > maxval)
  133.     return_error(e_rangecheck);
  134.     *pvalue = ival;
  135.     return code;
  136. }
  137.  
  138. /* Get a float parameter from a dictionary. */
  139. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  140. int
  141. dict_float_param(const ref * pdict, const char *kstr,
  142.          floatp defaultval, float *pvalue)
  143. {
  144.     ref *pdval;
  145.  
  146.     if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  147.     *pvalue = defaultval;
  148.     return 1;
  149.     }
  150.     switch (r_type(pdval)) {
  151.     case t_integer:
  152.         *pvalue = pdval->value.intval;
  153.         return 0;
  154.     case t_real:
  155.         *pvalue = pdval->value.realval;
  156.         return 0;
  157.     }
  158.     return_error(e_typecheck);
  159. }
  160.  
  161. /* Get an integer array from a dictionary. */
  162. /* See idparam.h for specification. */
  163. int
  164. dict_int_array_check_param(const ref * pdict, const char *kstr, uint len,
  165.                int *ivec, int under_error, int over_error)
  166. {
  167.     ref *pdval;
  168.     const ref *pa;
  169.     int *pi = ivec;
  170.     uint size;
  171.     int i;
  172.  
  173.     if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0)
  174.     return 0;
  175.     if (!r_has_type(pdval, t_array))
  176.     return_error(e_typecheck);
  177.     size = r_size(pdval);
  178.     if (size > len)
  179.     return_error(over_error);
  180.     pa = pdval->value.const_refs;
  181.     for (i = 0; i < size; i++, pa++, pi++) {
  182.     /* See dict_int_param above for why we allow reals here. */
  183.     switch (r_type(pa)) {
  184.         case t_integer:
  185.         if (pa->value.intval != (int)pa->value.intval)
  186.             return_error(e_rangecheck);
  187.         *pi = (int)pa->value.intval;
  188.         break;
  189.         case t_real:
  190.         if (pa->value.realval < min_int ||
  191.             pa->value.realval > max_int ||
  192.             pa->value.realval != (int)pa->value.realval
  193.             )
  194.             return_error(e_rangecheck);
  195.         *pi = (int)pa->value.realval;
  196.         break;
  197.         default:
  198.         return_error(e_typecheck);
  199.     }
  200.     }
  201.     return (size == len || under_error >= 0 ? size :
  202.         gs_note_error(under_error));
  203. }
  204. int
  205. dict_int_array_param(const ref * pdict, const char *kstr,
  206.              uint maxlen, int *ivec)
  207. {
  208.     return dict_int_array_check_param(pdict, kstr, maxlen, ivec,
  209.                       0, e_limitcheck);
  210. }
  211. int
  212. dict_ints_param(const ref * pdict, const char *kstr,
  213.         uint len, int *ivec)
  214. {
  215.     return dict_int_array_check_param(pdict, kstr, len, ivec,
  216.                       e_rangecheck, e_rangecheck);
  217. }
  218.  
  219. /* Get a float array from a dictionary. */
  220. /* Return the element count if OK, <0 if invalid. */
  221. /* If the parameter is missing, then if defaultvec is NULL, return 0; */
  222. /* if defaultvec is not NULL, copy it into fvec (maxlen elements) */
  223. /* and return maxlen. */
  224. int
  225. dict_float_array_check_param(const ref * pdict, const char *kstr,
  226.                  uint len, float *fvec, const float *defaultvec,
  227.                  int under_error, int over_error)
  228. {
  229.     ref *pdval;
  230.     uint size;
  231.     int code;
  232.  
  233.     if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  234.     if (defaultvec == NULL)
  235.         return 0;
  236.     memcpy(fvec, defaultvec, len * sizeof(float));
  237.  
  238.     return len;
  239.     }
  240.     if (!r_has_type(pdval, t_array))
  241.     return_error(e_typecheck);
  242.     size = r_size(pdval);
  243.     if (size > len)
  244.     return_error(over_error);
  245.     code = float_params(pdval->value.refs + size - 1, size, fvec);
  246.     return (code < 0 ? code :
  247.         size == len || under_error >= 0 ? size :
  248.         gs_note_error(under_error));
  249. }
  250. int
  251. dict_float_array_param(const ref * pdict, const char *kstr,
  252.                uint maxlen, float *fvec, const float *defaultvec)
  253. {
  254.     return dict_float_array_check_param(pdict, kstr, maxlen, fvec,
  255.                     defaultvec, 0, e_limitcheck);
  256. }
  257. int
  258. dict_floats_param(const ref * pdict, const char *kstr,
  259.           uint maxlen, float *fvec, const float *defaultvec)
  260. {
  261.     return dict_float_array_check_param(pdict, kstr, maxlen, fvec, defaultvec,
  262.                     e_rangecheck, e_rangecheck);
  263. }
  264.  
  265. /*
  266.  * Get a procedure from a dictionary.  If the key is missing,
  267.  *      defaultval = false means substitute t__invalid;
  268.  *      defaultval = true means substitute an empty procedure.
  269.  * In either case, return 1.
  270.  */
  271. int
  272. dict_proc_param(const ref * pdict, const char *kstr, ref * pproc,
  273.         bool defaultval)
  274. {
  275.     ref *pdval;
  276.  
  277.     if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  278.     if (defaultval)
  279.         make_empty_const_array(pproc, a_readonly + a_executable);
  280.     else
  281.         make_t(pproc, t__invalid);
  282.     return 1;
  283.     }
  284.     check_proc(*pdval);
  285.     *pproc = *pdval;
  286.     return 0;
  287. }
  288.  
  289. /* Get a matrix from a dictionary. */
  290. int
  291. dict_matrix_param(const ref * pdict, const char *kstr, gs_matrix * pmat)
  292. {
  293.     ref *pdval;
  294.  
  295.     if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0)
  296.     return_error(e_typecheck);
  297.     return read_matrix(pdval, pmat);
  298. }
  299.  
  300. /* Get a UniqueID or XUID from a dictionary. */
  301. /* Return 0 if UniqueID, 1 if XUID, <0 if error. */
  302. /* If there is no uid, return default. */
  303. int
  304. dict_uid_param(const ref * pdict, gs_uid * puid, int defaultval,
  305.            gs_memory_t * mem, const i_ctx_t *i_ctx_p)
  306. {
  307.     ref *puniqueid;
  308.  
  309.     if (pdict == 0) {
  310.     uid_set_invalid(puid);
  311.     return defaultval;
  312.     }
  313.     /* In a Level 2 environment, check for XUID first. */
  314.     if (level2_enabled &&
  315.     dict_find_string(pdict, "XUID", &puniqueid) > 0
  316.     ) {
  317.     long *xvalues;
  318.     uint size, i;
  319.  
  320.     if (!r_has_type(puniqueid, t_array))
  321.         return_error(e_typecheck);
  322.     size = r_size(puniqueid);
  323.     if (size == 0)
  324.         return_error(e_rangecheck);
  325.     xvalues = (long *)gs_alloc_byte_array(mem, size, sizeof(long),
  326.                           "get XUID");
  327.  
  328.     if (xvalues == 0)
  329.         return_error(e_VMerror);
  330.     /* Get the values from the XUID array. */
  331.     for (i = 0; i < size; i++) {
  332.         const ref *pvalue = puniqueid->value.const_refs + i;
  333.  
  334.         if (!r_has_type(pvalue, t_integer)) {
  335.         gs_free_object(mem, xvalues, "get XUID");
  336.         return_error(e_typecheck);
  337.         }
  338.         xvalues[i] = pvalue->value.intval;
  339.     }
  340.     uid_set_XUID(puid, xvalues, size);
  341.     return 1;
  342.     }
  343.     /* If no UniqueID entry, set the UID to invalid, */
  344.     /* because UniqueID need not be present in all fonts, */
  345.     /* and if it is, the legal range is 0 to 2^24-1. */
  346.     if (dict_find_string(pdict, "UniqueID", &puniqueid) <= 0) {
  347.     uid_set_invalid(puid);
  348.     return defaultval;
  349.     } else {
  350.     if (!r_has_type(puniqueid, t_integer) ||
  351.         puniqueid->value.intval < 0 ||
  352.         puniqueid->value.intval > 0xffffffL
  353.         )
  354.         return_error(e_rangecheck);
  355.     /* Apparently fonts created by Fontographer often have */
  356.     /* a UniqueID of 0, contrary to Adobe's specifications. */
  357.     /* Treat 0 as equivalent to -1 (no UniqueID). */
  358.     if (puniqueid->value.intval == 0) {
  359.         uid_set_invalid(puid);
  360.         return defaultval;
  361.     } else
  362.         uid_set_UniqueID(puid, puniqueid->value.intval);
  363.     }
  364.     return 0;
  365. }
  366.  
  367. /* Check that a UID in a dictionary is equal to an existing, valid UID. */
  368. bool
  369. dict_check_uid_param(const ref * pdict, const gs_uid * puid)
  370. {
  371.     ref *puniqueid;
  372.  
  373.     if (uid_is_XUID(puid)) {
  374.     uint size = uid_XUID_size(puid);
  375.     uint i;
  376.  
  377.     if (dict_find_string(pdict, "XUID", &puniqueid) <= 0)
  378.         return false;
  379.     if (!r_has_type(puniqueid, t_array) ||
  380.         r_size(puniqueid) != size
  381.         )
  382.         return false;
  383.     for (i = 0; i < size; i++) {
  384.         const ref *pvalue = puniqueid->value.const_refs + i;
  385.  
  386.         if (!r_has_type(pvalue, t_integer))
  387.         return false;
  388.         if (pvalue->value.intval != uid_XUID_values(puid)[i])
  389.         return false;
  390.     }
  391.     return true;
  392.     } else {
  393.     if (dict_find_string(pdict, "UniqueID", &puniqueid) <= 0)
  394.         return false;
  395.     return (r_has_type(puniqueid, t_integer) &&
  396.         puniqueid->value.intval == puid->id);
  397.     }
  398. }
  399.